package io.shallwe.common.utils;

import com.jfinal.core.JFinal;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;

public class StrUtil extends StrKit {
    private static final Log log = Log.getLog(StrUtil.class);
    private static final String[] htmlChars = new String[]{"&", "<", ">", "'", "\""};
    private static final String[] escapeChars = new String[]{"&amp;", "&lt;", "&gt;", "&#39;", "&quot;"};

    public StrUtil() {
    }

    public static String urlDecode(String string) {
        try {
            return URLDecoder.decode(string, JFinal.me().getConstants().getEncoding());
        } catch (UnsupportedEncodingException var2) {
            log.error("urlDecode is error", var2);
            return string;
        }
    }

    public static String urlEncode(String string) {
        try {
            return URLEncoder.encode(string, JFinal.me().getConstants().getEncoding());
        } catch (UnsupportedEncodingException var2) {
            log.error("urlEncode is error", var2);
            return string;
        }
    }

    public static String urlRedirect(String redirect) {
        try {
            redirect = new String(redirect.getBytes(JFinal.me().getConstants().getEncoding()), "ISO8859_1");
        } catch (UnsupportedEncodingException var2) {
            log.error("urlRedirect is error", var2);
        }

        return redirect;
    }

    public static boolean areNotEmpty(String... strings) {
        if (strings != null && strings.length != 0) {
            String[] var1 = strings;
            int var2 = strings.length;

            for(int var3 = 0; var3 < var2; ++var3) {
                String string = var1[var3];
                if (string == null || "".equals(string)) {
                    return false;
                }
            }

            return true;
        } else {
            return false;
        }
    }

    public static String requireNonBlank(String string) {
        if (isBlank(string)) {
            throw new NullPointerException();
        } else {
            return string;
        }
    }

    public static String requireNonBlank(String string, String message) {
        if (isBlank(string)) {
            throw new NullPointerException(message);
        } else {
            return string;
        }
    }

    public static String obtainDefaultIfBlank(String string, String defaultValue) {
        return isBlank(string) ? defaultValue : string;
    }

    public static boolean isNotEmpty(String string) {
        return string != null && !string.equals("");
    }

    public static boolean isNotBlank(Object o) {
        return o == null ? false : notBlank(o.toString());
    }

    public static boolean match(String string, String regex) {
        Pattern pattern = Pattern.compile(regex, 2);
        Matcher matcher = pattern.matcher(string);
        return matcher.matches();
    }

    public static boolean isNumeric(String str) {
        if (str == null) {
            return false;
        } else {
            int i = str.length();

            char chr;
            do {
                --i;
                if (i < 0) {
                    return true;
                }

                chr = str.charAt(i);
            } while(chr >= '0' && chr <= '9');

            return false;
        }
    }

    public static boolean isDecimal(String str) {
        if (str == null) {
            return false;
        } else {
            int i = str.length();

            char chr;
            do {
                do {
                    --i;
                    if (i < 0) {
                        return true;
                    }

                    chr = str.charAt(i);
                } while(chr >= '0' && chr <= '9');
            } while(chr == '.');

            return false;
        }
    }

    public static boolean isEmail(String email) {
        return Pattern.matches("\\w+@(\\w+.)+[a-z]{2,3}", email);
    }

    public static boolean isMobileNumber(String phoneNumber) {
        return Pattern.matches("^(1[3,4,5,6,7,8,9])\\d{9}$", phoneNumber);
    }

    public static String uuid() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    public static Set<String> splitToSet(String src, String regex) {
        if (src == null) {
            return null;
        } else {
            String[] strings = src.split(regex);
            Set<String> set = new HashSet();
            String[] var4 = strings;
            int var5 = strings.length;

            for(int var6 = 0; var6 < var5; ++var6) {
                String s = var4[var6];
                if (!isBlank(s)) {
                    set.add(s.trim());
                }
            }

            return set;
        }
    }

    public static String escapeHtml(String content) {
        return isBlank(content) ? content : StringUtils.replaceEach(unEscapeHtml(content), htmlChars, escapeChars);
    }

    public static String unEscapeHtml(String content) {
        return isBlank(content) ? content : StringUtils.replaceEach(content, escapeChars, htmlChars);
    }

    public static void main(String[] args) {
        String url = "http://www.baidu.com?username=aaa";
        System.out.println(urlEncode(url));
    }
}
